import { ref } from 'vue'

export const toasts = ref([])
let nextId = 0

export function showToast(message, type = 'success', duration = 5000) {
    const id = nextId++
    const index = toasts.value.length

    const toast = {
        id,
        message,
        type,
        index
    }

    toasts.value.push(toast)

    setTimeout(() => {
        removeToast(id)
    }, duration)
}

function removeToast(id) {
    const index = toasts.value.findIndex(t => t.id === id)
    if (index > -1) {
        toasts.value.splice(index, 1)
        toasts.value.forEach((toast, i) => {
            toast.index = i
        })
    }
} 